using DA.AnimGraph;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {

    internal class InspectorField_Vector2Link : InspectorField<AGAFieldModel_Vector2Link> {

        Foldout foldout;
        EnumField linkSourceField;
        Vector2Field defaultValueField;
        PopupField<AGA_Blackboard.Entry> blackboardField;
        Toggle useClampField;
        Vector2Field clampRangeX;
        Vector2Field clampRangeY;

        protected override void BuildUI() {
            foldout = new Foldout() {
                text = $"[vector2]  {labelText}"
            };

            linkSourceField = new EnumField("source");
            linkSourceField.RegisterValueChangedCallback(evt => OnModify());
            linkSourceField.Init(source.sourceType);

            defaultValueField = new Vector2Field("value");
            defaultValueField.SetSubFieldLeftPadding();
            defaultValueField.RegisterValueChangedCallback(evt => OnModify());
            defaultValueField.value = source.defaultValue;

            blackboardField = new PopupField<AGA_Blackboard.Entry>("blackboard");
            blackboardField.SetSubFieldLeftPadding();
            blackboardField.RegisterValueChangedCallback(evt => OnModify());

            useClampField = new Toggle("use clamp");
            useClampField.SetSubFieldLeftPadding();
            useClampField.RegisterValueChangedCallback(evt => OnModify());
            useClampField.value = source.useClamp;

            clampRangeX = new Vector2Field("x range");
            clampRangeX.SetSubFieldLeftPadding(2);
            clampRangeX.RegisterValueChangedCallback(evt => OnModify());
            clampRangeX.value = source.clampRangeX;

            clampRangeY = new Vector2Field("y range");
            clampRangeY.SetSubFieldLeftPadding(2);
            clampRangeY.RegisterValueChangedCallback(evt => OnModify());
            clampRangeY.value = source.clampRangeY;
        }

        protected override void DrawUI() {
            Add(foldout);
            foldout.Add(linkSourceField);
            switch (source.sourceType) {
                case LinkSourceType.Const:
                    foldout.Add(defaultValueField);
                    break;
                case LinkSourceType.Blackboard:
                    // blackboard
                    var choices = ageContext.GetBlackboardEntriesInType(AGABlackboardType.Vector2);
                    choices.Insert(0, new AGA_Blackboard.Entry() {
                        name = "(empty)",
                        uuid = default,
                        type = AGABlackboardType.Vector2,
                    });
                    var selectIndex = choices.FindIndex(p => p.uuid == source.blackboardKey);
                    if (selectIndex < 0) {
                        selectIndex = 0;
                    }
                    blackboardField.choices = choices;
                    blackboardField.index = selectIndex;
                    blackboardField.formatListItemCallback = p => p.name; // 决定每个选项的文本
                    blackboardField.formatSelectedValueCallback = p => p.name; // 决定显示在下拉框中的文本
                    foldout.Add(blackboardField);
                    
                    // clamp
                    foldout.Add(useClampField);
                    if (useClampField.value) {
                        foldout.Add(clampRangeX);
                        foldout.Add(clampRangeY);
                    }
                    break;
            }

           
        }

        protected override void ClearUI() {
            foldout?.Clear();
            Clear();
        }


        private void OnModify() {
            source.sourceType = (LinkSourceType)linkSourceField.value;
            source.defaultValue = defaultValueField.value;
            source.blackboardKey = blackboardField.value.uuid;
            source.useClamp = useClampField.value;
            source.clampRangeX = clampRangeX.value;
            source.clampRangeY = clampRangeY.value;

            inspectorChangeCallback?.Invoke();
            ClearUI();
            DrawUI();
        }
    }
}
